package com.aliang.shopping.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.aliang.shopping.config.OssConfig;
import com.aliang.shopping.mapper.CartMapper;
import com.aliang.shopping.mapper.GoodsSpecMapper;
import com.aliang.shopping.model.converter.GoodsConverter;
import com.aliang.shopping.model.po.Cart;
import com.aliang.shopping.model.po.GoodsSpec;
import com.aliang.shopping.model.vo.cart.CartShowVO;
import com.aliang.shopping.model.vo.cart.CartVO;
import com.aliang.shopping.service.CartService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ShiBaLover
 * @description 针对表【cart(购物车表)】的数据库操作Service实现
 * @createDate 2025-01-04 10:54:50
 */
@Service
@AllArgsConstructor
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart>
        implements CartService {

    private final OssConfig ossConfig;
    private final GoodsConverter goodsConverter;
    private final GoodsSpecMapper goodsSpecMapper;

    @Override
    public List<CartShowVO> getCartShowVO(Long userId) {
        List<CartShowVO> cartShowVO = this.baseMapper.getCartShowVO(userId);
        if (cartShowVO.isEmpty())
            // 数据为空，返回空数组
            return Collections.emptyList();
        //获取规格Id
        Set<Long> specList = new HashSet<>();
        cartShowVO.forEach(showVo -> {
            showVo.getCartInfo().forEach(cartVO -> {
                String goodsSpecName = cartVO.getGoodsSpecName();
                List<Long> list = JSONUtil.toList(goodsSpecName, Long.class);
                specList.addAll(list);
            });
        });
        // 查询规格信息
        Map<Long, String> specMap = this.goodsSpecMapper.selectList(
                new LambdaQueryWrapper<GoodsSpec>()
                        .select(GoodsSpec::getId, GoodsSpec::getSpecName)
                        .in(GoodsSpec::getId, specList)
        ).stream().collect(Collectors.toMap(GoodsSpec::getId, GoodsSpec::getSpecName));
        // 设置商品图片及规格名字
        cartShowVO.forEach(showVo -> {
            showVo.getCartInfo().forEach(cartVO -> {
                if (StrUtil.isNotBlank(cartVO.getGoodsImage())) {
                    // 获取图片第一张
                    String oneMainImages = goodsConverter.getOneMainImages(cartVO.getGoodsImage());
                    if (StrUtil.isNotBlank(oneMainImages))
                        //设置完成路径
                        cartVO.setGoodsImage(ossConfig.getUrl() + oneMainImages);
                }
                String goodsSpecName = cartVO.getGoodsSpecName();
                List<Long> list = JSONUtil.toList(goodsSpecName, Long.class);
                // 拼接规格名称
                List<String> collect = list.stream().map(specMap::get).collect(Collectors.toList());
                String join = CollectionUtil.join(collect, ",");
                cartVO.setGoodsSpecName(join);
                //计算折扣价
                cartVO.setGoodsScopePrice(cartVO.getGoodsPrice().multiply(cartVO.getGoodsScope())
                        //设置2位小数
                        .setScale(2, RoundingMode.HALF_UP));

            });
        });
        return cartShowVO;

    }

    @Override
    public List<CartShowVO> getCartShowVOByIds(List<Long> cartIds, Long userId) {

        return this.baseMapper.getCartShowVOByIds(cartIds, userId);
    }
}




